package com.dissertation.action;

import com.alibaba.fastjson.JSON;
import com.dissertation.annotation.Category;
import com.dissertation.model.Assets;
import com.dissertation.pojo.Message;
import com.dissertation.service.impl.SaveAssetsImpl;
import com.dissertation.service.impl.SelectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@RestController
public class AssetsController {

    @Autowired
    SelectService selectService;

    @Autowired
    SaveAssetsImpl saveAssets;

    @PostMapping( "{assetName}" )
    Message reflectPOST( @PathVariable String assetName, HttpServletRequest request ) throws IOException {
        try {
            Class<?> clazz = Class.forName( "com.dissertation.model." + assetName );
            StringBuilder builder = getStringBuilder( request );
            Object o = JSON.parseObject( builder.toString(), clazz );
            if ( o instanceof Assets ) {
                return saveAssets.save( (Assets) o );
            } else {
                return Message.createErr( "对不起参数不合法" );
            }
        } catch ( ClassNotFoundException e ) {
            return Message.createErr( "对不起，不合法的路径" );
        }
    }


    @GetMapping( "{assetName}" )
    Message reflectGET( @PathVariable String assetName ) throws IOException {
        try {
            Class<?> clazz = Class.forName( "com.dissertation.model." + assetName );
            Category category = clazz.getAnnotation( Category.class );
            if ( category == null ) {
                return Message.createErr( "对不起，缺少必要的注释信息，请检查类" );
            }
            return selectService.findByName( category.categoryTreeName(),
                    category.categoryLeafName(), clazz );
        } catch ( ClassNotFoundException e ) {
            return Message.createErr( "对不起，不合法的路径" );
        }
    }

    @PutMapping( "{assetName}" )
    Message reflectPUT( @PathVariable String assetName, HttpServletRequest request ) throws IOException {
        try {
            Class<?> clazz = Class.forName( "com.dissertation.model." + assetName );
            StringBuilder builder = getStringBuilder( request );
            Object o = JSON.parseObject( builder.toString(), clazz );
            if ( o instanceof Assets ) {
                return saveAssets.update( (Assets) o );
            } else {
                return Message.createErr( "对不起参数不合法" );
            }
        } catch ( ClassNotFoundException e ) {
            return Message.createErr( "对不起，不合法的路径" );
        }
    }



    private StringBuilder getStringBuilder( HttpServletRequest request ) throws IOException {
        BufferedReader reader = request.getReader();
        StringBuilder builder = new StringBuilder();
        String line;
        while ( ( line = reader.readLine() ) != null ) {
            builder.append( line );
        }
        reader.close();
        return builder;
    }

    @GetMapping( "{assetName}/date" )
    Message query( @PathVariable String assetName,
                   @RequestParam( defaultValue = "0000" ) String begin,
                   @RequestParam( defaultValue = "9999" ) String end,
                   String current ) {
        try {
            Class<?> clazz = Class.forName( "com.dissertation.model." + assetName );
            Category category = clazz.getAnnotation( Category.class );
            return selectService.findByNameAndDate( category.categoryTreeName(),
                    category.categoryLeafName(), clazz, begin, end , current);
        } catch ( ClassNotFoundException e ) {
            return Message.createErr( "对不起，请检查url" );
        }

    }

    @GetMapping( "{assetName}/{id}" )
    Message query( @PathVariable String assetName, @PathVariable String id ,HttpServletResponse response) {
        try {
            Class<?> clazz = Class.forName( "com.dissertation.model." + assetName );
            Category category = clazz.getAnnotation( Category.class );
            return selectService.findByNameAndId( category.categoryTreeName(),
                    category.categoryLeafName(), clazz, id );
        } catch ( ClassNotFoundException e ) {
            return Message.createErr( "对不起，请检查url" );
        }
    }

}
